#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Mvc;
using xConnected.ActionFilter;
using xConnected.Core.Interfaces.Service;
using xConnected.Core.Model;
using xConnected.Helpers;
using xConnected.Infrastructure;

#endregion

namespace xConnected.Controllers
{
    [SetCulture]
    public abstract class BaseController<T> : Controller where T : PersistentEntity
    {
        protected IService<T> Service;


        #region JSON

        public virtual JsonResult GetAll()
        {
            var json = Service.GetAll().ToList().Select(o => o.Serialize()).ToList();
            return new JsonResult { Data = json, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public virtual JsonResult GetById(int id)
        {
            return new JsonResult { Data = Service.GetById(id).Serialize(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public virtual JsonResult SaveOrUpdate(T entity)
        {
            var result = Service.SaveOrUpdate(entity);
            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public virtual JsonResult Delete(int id)
        {
            var entity = Service.GetById(id);
            Service.Delete(entity);
            return new JsonResult { Data = "", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public virtual ViewResult SinglePage()
        {
            return View();
        }

        #endregion
    }
}